package com.changhai.diabetes.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.changhai.diabetes.model.NutritionSuifang;
import com.changhai.diabetes.model.NutritionSurvey;
import com.changhai.diabetes.model.NutritionTreatment;
import com.changhai.diabetes.model.OpertResultMsg;
import com.changhai.diabetes.service.NutritionService;

/**
 * 营养
 * 
 * @author Andy
 * 
 */
public class NutritionAction extends BaseAction {

	private static final long serialVersionUID = 4392936553263004630L;
	private OpertResultMsg opertMsg;
	private Long patientid;
	private NutritionSurvey nutriSurvey;
	private NutritionTreatment nutriTreat;
	private NutritionSuifang nutriSuifang;
	private NutritionService nutritionService;
	private Integer recommend;
	private String starttime;
	private String endtime;
	private Integer flag = 0;

	public String execute() throws Exception {
		return super.execute();
	}

	public String query() {
		return "input";
	}

	public String editNutritionSurvey() {
		if (patientid != null && patientid > 0) {
			try {
				nutriSurvey = nutritionService.searchSurveyByPatientId(patientid);
				tbList = this.getTableForEnter(patientid, flag);
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无营养信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditNutritionSurvey() {
		if (patientid == null || patientid <= 0 || nutriSurvey == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				nutritionService.saveEditNutritionSurvey(nutriSurvey);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "营养调查", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editNutritionSurvey();
	}

	public String saveNewNutritionSurvey() {
		if (patientid == null || patientid <= 0 || nutriSurvey == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				nutritionService.saveNewNutritionSurvey(nutriSurvey);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("insert", "营养调查", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editNutritionSurvey();
	}

	public String editNutritionTreat() {
		if (patientid != null && patientid > 0) {
			try {
				tbList = this.getTableForEnter(patientid, flag);
				Map<String, Object> map = new HashMap<String, Object>();
				map = nutritionService.searchTreatByPatientId(patientid);
				if (map != null) {
					nutriTreat = (NutritionTreatment) map.get("treat");
					recommend = (Integer) map.get("recommend");
					if (nutriTreat == null || recommend == null) {
						throw new RuntimeException();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无膳食推荐信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditNutritionTreat() throws IOException {
		PrintWriter pw = ServletActionContext.getResponse().getWriter();
		String res = "0";
		if (patientid != null && patientid > 0) {
			try {
				NutritionTreatment treat = new NutritionTreatment();
				treat.setStarttime(starttime);
				treat.setEndtime(endtime);
				treat.setPatientid(patientid);
				nutritionService.saveEditNutritionTreatTime(treat);
				res = "1";
				commonService.writeLogToFile("update", "营养治疗", patientid);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		pw.write(res);
		return NONE;
	}

	public String editNutritionSuifang() {
		if (patientid != null && patientid > 0) {
			try {
				nutriSuifang = nutritionService.searchSuifangByPatientId(patientid);
				tbList = this.getTableForEnter(patientid, flag);
			} catch (Exception e) {
				e.printStackTrace();
				opertMsg = new OpertResultMsg(Long.valueOf(1), "暂无营养信息!");
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditNutritionSuifang() {
		if (patientid == null || patientid <= 0 || nutriSuifang == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				nutritionService.saveEditNutritionSuifang(nutriSuifang);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "营养随访", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editNutritionSuifang();
	}

	public String saveNewNutritionSuifang() {
		if (patientid == null || patientid <= 0 || nutriSuifang == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				nutritionService.saveNewNutritionSuifang(nutriSuifang);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("insert", "营养随访", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editNutritionSuifang();
	}

	public String getNutritionRecomInfo() throws IOException{
		PrintWriter wr = ServletActionContext.getResponse().getWriter();
		String res = "0";
		if (patientid != null && patientid > 0) {
			try {
				nutriSurvey = nutritionService.searchSurveyByPatientId(patientid);
				if (nutriSurvey != null) {
					JSONArray json = JSONArray.fromObject(nutriSurvey);
					res = json != null ? json.toString() : "0";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		wr.write(res);
		return NONE;
	}
	
	public OpertResultMsg getOpertMsg() {
		return opertMsg;
	}

	public void setOpertMsg(OpertResultMsg opertMsg) {
		this.opertMsg = opertMsg;
	}

	public Long getPatientid() {
		return patientid;
	}

	public void setPatientid(Long patientid) {
		this.patientid = patientid;
	}

	public NutritionSurvey getNutriSurvey() {
		return nutriSurvey;
	}

	public void setNutriSurvey(NutritionSurvey nutriSurvey) {
		this.nutriSurvey = nutriSurvey;
	}

	public NutritionService getNutritionService() {
		return nutritionService;
	}

	public void setNutritionService(NutritionService nutritionService) {
		this.nutritionService = nutritionService;
	}

	public NutritionTreatment getNutriTreat() {
		return nutriTreat;
	}

	public void setNutriTreat(NutritionTreatment nutriTreat) {
		this.nutriTreat = nutriTreat;
	}

	public Integer getRecommend() {
		return recommend;
	}

	public void setRecommend(Integer recommend) {
		this.recommend = recommend;
	}

	public String getStarttime() {
		return starttime;
	}

	public void setStarttime(String starttime) {
		this.starttime = starttime;
	}

	public String getEndtime() {
		return endtime;
	}

	public void setEndtime(String endtime) {
		this.endtime = endtime;
	}

	public NutritionSuifang getNutriSuifang() {
		return nutriSuifang;
	}

	public void setNutriSuifang(NutritionSuifang nutriSuifang) {
		this.nutriSuifang = nutriSuifang;
	}

	public Integer getFlag() {
		return flag;
	}

	public void setFlag(Integer flag) {
		this.flag = flag;
	}
}
